/**
 * Copyright (C), 2020, 安徽贝慕信息科技有限公司
 * FileName: DealerCompanyService
 * Author:   Allen
 * Date:     2020/8/26 21:53
 * Description: 商家基本信息登记处理服务类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.bim.marvel.openapi.core.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bim.marvel.openapi.client.query.DealerCompanyQuery;
import com.bim.marvel.openapi.core.dao.DealerCompanyDAO;
import com.bim.marvel.openapi.core.entity.DealerCompanyDO;
import com.bim.marvel.openapi.core.entity.DealerUserDO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

/**
 * 〈商家基本信息登记处理服务类〉
 *
 * @author Allen
 * @date 2020/8/26
 * @since 1.0.0
 */
@Service
public class DealerCompanyService extends ServiceImpl<DealerCompanyDAO, DealerCompanyDO> {

    /**
     * 注入DealerUserService
     */
    @Autowired
    private DealerUserService dealerUserService;

    /**
     * 保存商家申请接入号时必须填写的基本信息(同时更新当前填写用户的所属机构信息)
     * @param dealerCompanyQuery 商家基本信息入参
     * @return 返回系统生成的商家编号
     */
    @CacheEvict(value = "getDealerUserInfo", key = "'userInfo:' + #dealerCompanyQuery.loginName")
    public String saveDealerCompany(DealerCompanyQuery dealerCompanyQuery) {
        DealerCompanyDO dealerCompanyDO = new DealerCompanyDO();
        BeanUtils.copyProperties(dealerCompanyQuery, dealerCompanyDO);
        save(dealerCompanyDO);
        DealerUserDO dealerUserDO = dealerUserService.getById(dealerCompanyQuery.getUserId());
        dealerUserDO.setCompanyCode(dealerCompanyDO.getCompanyCode());
        dealerUserService.updateById(dealerUserDO);
        return dealerCompanyDO.getCompanyCode();
    }
}
